package models.re.aaa;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;
/**
	 * data structure for pagerank vector
	 * @author Raphael
	 */
public class NodeScore extends HashMap<String,Double>{

	
	private static final long serialVersionUID = 1L;
	
	private class NodeScoreComparator implements Comparator<String> {
		
		private NodeScore ns;

		public NodeScoreComparator(NodeScore ns) {
			this.ns=ns;
		}

		@Override
		public int compare(String o1, String o2) {
			// TODO Auto-generated method stub
			if(-Double.compare(ns.get(o1), ns.get(o2))==0) return o1.compareTo(o2);
			else return -Double.compare(ns.get(o1), ns.get(o2));
		}
	}

	public void addElement(String name, Double score) {
		this.put(name,score);
	}
	
	public void removeElement(String name) {
		this.remove(name);
	}
	
	public int length() {
		return this.size();
	}
	
	public Double find(String name) {
		return this.get(name);
	}
	
	public void updateScore(String name, Double score) {
		this.remove(name);
		this.put(name, score);
	}
	
	public Double sumScore() {
		Double total=0.0;
		for(Double score: this.values()) total+=score;
		return total;
	}
	
	public TreeMap<String,Double> sortByScore() {
		NodeScoreComparator cp = new NodeScoreComparator(this);
		TreeMap<String,Double> tm = new TreeMap<String,Double>(cp);
		tm.putAll(this);
		return tm;
	}

}
